<template>
  <!-- 搜索区域 -->
  <ContentWrap>
    <ElForm
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="字典名称" prop="dictType">
        <el-select v-model="queryParams.dictType" @change="handleDictTypeChange" class="!w-240px">
          <el-option
            v-for="item in dictTypeList"
            :key="item.type"
            :label="item.name"
            :value="item.type">
            <span>{{ item.name }}（{{ item.type }}）</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="字典标签" prop="label">
        <el-input
          v-model="queryParams.label"
          placeholder="请输入字典标签"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable class="!w-240px">
          <el-option
            v-for="dict in common_enable_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          重置
        </el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['system:dict:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" />
          新增
        </el-button>
        <el-button plain @click="refreshDict">
          <Icon class="mr-5px" icon="ep:refresh" />
          刷新字典缓存
        </el-button>
      </el-form-item>
    </ElForm>
  </ContentWrap>

  <!-- 列表区域 -->
  <ContentWrap>
    <el-table v-loading="listLoading" :data="list">
      <el-table-column label="字典编号" prop="id" />
      <el-table-column label="字典标签" prop="label" width="180" show-overflow-tooltip />
      <el-table-column label="字典键值" prop="value" width="180" show-overflow-tooltip />
      <el-table-column label="字典排序" prop="sort" />
      <el-table-column label="状态" prop="status">
        <template #default="scope">
          <dict-tag :options="common_enable_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="颜色类型" prop="colorType">
        <template #default="scope">
          <span v-if="scope.row.colorType === 'default' || scope.row.colorType === ''">{{
            scope.row.colorType
          }}</span>
          <el-tag v-else :disable-transitions="true" :type="scope.row.colorType">{{
            scope.row.colorType
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="CSS Class" prop="cssClass" />
      <el-table-column label="备注" prop="remark" show-overflow-tooltip />
      <el-table-column label="创建时间" prop="createTime" width="180" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['system:dict:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['system:dict:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页区域 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <DictDataForm ref="editFormRef" @success="getList" />
</template>

<script setup lang="ts">
import { useDictStore } from '@/stores/modules/dict'
import * as DictDataApi from '@/api/system/dict/dict_data'
import * as DictTypeApi from '@/api/system/dict/dict_type'
import DictDataForm from './DictDataForm.vue'

defineOptions({ name: 'SystemDictData' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const route = useRoute() // 当前路由
// const { currentRoute } = useRouter() //这种方式也可以获取当前路由

const { common_enable_status } = useIntDict(DICT_TYPE.common_enable_status)

const queryParams = reactive({
  dictType: route.params.dictType as string,
  label: undefined,
  status: undefined,
  pageNo: 1,
  pageSize: 50
})
const queryFormRef = ref()
const dictTypeList = ref<DictTypeApi.DictTypeVO[]>() // 字典类型的列表

const listLoading = ref(false)

const list = ref([]) // 列表数据
const total = ref(0) // 列表数据总条数

const getList = async () => {
  listLoading.value = true
  try {
    const data = await DictDataApi.getDictDataPage(queryParams)
    list.value = data.content
    total.value = data.totalElement
  } finally {
    listLoading.value = false
  }
}

const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const handleDictTypeChange = () => {
  handleQuery()
}

const handleDelete = async (id: number) => {
  try {
    // 弹出确认框
    await message.delConfirm()
    // 发起删除请求
    await DictDataApi.deleteDictData(id)
    // 成功提示
    message.success(t('common.delSuccess'))
    // 刷新列表(页码不变)
    await getList()
  } catch {}
}

const refreshDict = async () => {
  try {
    await message.confirm('是否确认刷新数据字典缓存？')
    useDictStore()
      .resetDict()
      .then(() => {
        message.success(t('common.operateSuccess'))
      })
  } catch {}
}

const editFormRef = ref<InstanceType<typeof DictDataForm>>()
const openForm = (type: string, id?: number) => {
  editFormRef.value?.open(type, id, queryParams.dictType)
}

onMounted(async () => {
  getList()
  dictTypeList.value = await DictTypeApi.getDictTypeSimpleList()
})
</script>

<style scoped lang="scss"></style>
